package com.prs.crm.dao.crm.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.crm.RivalDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.Rival;

public class RivalDaoImpl extends BaseDaoImpl<Rival> implements RivalDao{

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Rival> getRivalByName(final String name) {
		// TODO Auto-generated method stub
		return (Collection<Rival>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Rival.class);
				criteria.add(Restrictions.eq("name", name));
				return criteria.list();
			}
			
		});
	}

	@Override
	public Collection<?> getReportLevelSource() {
		// TODO Auto-generated method stub
		return (Collection<?>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Rival.class);
				ProjectionList projectionlist=Projections.projectionList();  
	        	projectionlist.add(Projections.groupProperty("rivalLevel"));
	        	projectionlist.add(Projections.rowCount());
	        	criteria.setProjection(projectionlist);
				return criteria.list();
			}
			
		});
	}

	@Override
	public Collection<?> getReportNatureSource() {
		// TODO Auto-generated method stub
		return (Collection<?>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Rival.class);
				ProjectionList projectionlist=Projections.projectionList();  
	        	projectionlist.add(Projections.groupProperty("rivalNature"));
	        	projectionlist.add(Projections.rowCount());
	        	criteria.setProjection(projectionlist);
				return criteria.list();
			}
			
		});
	}

}
